﻿
#region NameSpace
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using UserManage;
using Eb.MessageBox;
#endregion

public partial class ucRoleDisplay : System.Web.UI.UserControl
{
    UserManager _userRoles = new UserManager();

    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
        btnBack.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnBack.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");

        btnUpdate.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnUpdate.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");

        if (! IsPostBack)
        GetRoles();
    }

    protected void btnMove1_Click(object sender, EventArgs e)
    {
        if (lstAllRoles.SelectedIndex == -1)
        {
            MessageBox.Show("Select Role");
            //Response.Write("<script>alert('Select Role')</script>");
            return;
        }
        lstSelectedRoles.Items.Add(new ListItem(lstAllRoles.SelectedItem.ToString(), lstAllRoles.SelectedValue));
        lstAllRoles.Items.RemoveAt(lstAllRoles.SelectedIndex);
    }

    protected void btnMove2_Click(object sender, EventArgs e)
    {
        int flag;
        for (int i = 0; i < lstAllRoles.Items.Count; i++)
        {
            flag = 0;
            for (int j = 0; j < lstSelectedRoles.Items.Count; j++)
                if (lstAllRoles.Items[i].Value == lstSelectedRoles.Items[j].Value)
                    flag = 1;

            if (flag == 0)
            {
                lstSelectedRoles.Items.Add(new ListItem(lstAllRoles.Items[i].Text, lstAllRoles.Items[i].Value));
            }
        }
        lstAllRoles.Items.Clear();

    }

    protected void btnMove3_Click(object sender, EventArgs e)
    {
        int flag;
        for (int i = 0; i < lstSelectedRoles.Items.Count; i++)
        {
            flag = 0;
            for (int j = 0; j < lstAllRoles.Items.Count; j++)
                if (lstSelectedRoles.Items[i].Value == lstAllRoles.Items[j].Value)
                    flag = 1;

            if (flag == 0)
            {
                lstAllRoles.Items.Add(new ListItem(lstSelectedRoles.Items[i].Text, lstSelectedRoles.Items[i].Value));
            }
        }
        lstSelectedRoles.Items.Clear();
    }

    protected void btnMove4_Click(object sender, EventArgs e)
    {
        if (lstSelectedRoles.SelectedIndex == -1)
        {
            MessageBox.Show("Select Role");
            //Response.Write("<script>alert('Select Role')</script>");
            return;
        }
        lstAllRoles.Items.Add(new ListItem(lstSelectedRoles.SelectedItem.ToString(), lstSelectedRoles.SelectedValue));
        lstSelectedRoles.Items.RemoveAt(lstSelectedRoles.SelectedIndex);
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Session["UserManager_Control"] = "ucUserSearch.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            AssignRoles();
            //  RemoveRoles();
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Assign Roles Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }

    #endregion

    #region Methods
    void GetRoles()
    {
        string condition = "T1.UM_UserID = '" + Session["UserManager_PersonalID"] + "' and T1.UM_RoleID = T2.UR_RoleID";
        DataSet ds= _userRoles.GetData("UserRolesMap", "UserRolesList", condition);
        lstSelectedRoles.DataSource = ds;
        lstSelectedRoles.DataTextField = "UR_RoleName";
        lstSelectedRoles.DataValueField = "UR_RoleID";
        lstSelectedRoles.DataBind();

//        SELECT     T1.*,T2.* 
//FROM        UserRolesList as T1,UserRolesMap as T2 where T1.UR_RoleID not in (select UM_RoleID from UserRolesMap where UM_UserID = 25)

        condition = "UR_RoleID not in (select UM_RoleID from UserRolesMap where UM_UserID = '" + Session["UserManager_PersonalID"] + "')";
        ds.Clear();
        ds = _userRoles.GetData("UserRolesList", condition);
        lstAllRoles.DataSource = ds;
        lstAllRoles.DataTextField = "UR_RoleName";
        lstAllRoles.DataValueField = "UR_RoleID";
        lstAllRoles.DataBind();     

    }

    void Update()
    {
        bool result=true;
        if (lstSelectedRoles.Items.Count == 0)
        {

            //Response.Write("<script>alert('Add Roles To The List')</script>");
            return;
        }
        for (int i = 0; i < lstSelectedRoles.Items.Count; i++)
        {
            result= _userRoles.AssignRole(Session["UserManager_PersonalID"].ToString(), lstSelectedRoles.Items[i].Value);
                          
        }
        //if (result)
       // Response.Write("<script>alert('Updated')</script>");
        MessageBox.Show("Updated Successfully.");
    }

    void AssignRoles()
    {
        bool result = true;
        if (lstSelectedRoles.Items.Count == 0)
        {
            MessageBox.Show("Add role to the list");
            //Response.Write("<script>alert('Add Roles To The List')</script>");
            return;
        }
        for (int i = 0; i < lstSelectedRoles.Items.Count; i++)
        {
            result = _userRoles.AssignRole(Session["UserManager_PersonalID"].ToString(), lstSelectedRoles.Items[i].Value);

        }        

        //if (result)
       // Response.Write("<script>alert('Updated')</script>");
        MessageBox.Show("Updated Successfully");
    }
    #endregion
}
